package com.yx.cloud.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.iot.product.sweeper.bean.Room;
import com.yx.cloud.R;
import com.yx.cloud.utils.RomUtils;
import com.iot.common.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 房间选择adapter
 */
public class RoomEditAdapter extends RecyclerView.Adapter {
    private final static String TAG = "RoomEditAdapter";
    private Context mContext;
    private List<Room> roomList;
    private Drawable drawable;
    private int selectCount = 0;


    public RoomEditAdapter(Context context) {
        mContext = context;
        roomList=new ArrayList<>();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.room_edit_adapter, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    public void updateData(List<Room> list, List<Integer> ids) {
        this.roomList = list;

        //计算当前选择的房间数
        selectCount = 0;
        for (int i = 0; i < ids.size(); i++) {
            for (int j = 0; j < roomList.size(); j++) {
                if (roomList.get(j).getId() == ids.get(i)) {
                    selectCount++;
                    roomList.get(j).setOrder(selectCount);
                    break;
                }
            }
        }

        notifyDataSetChanged();
    }

    public List<Integer> getSelectRoom() {
        List<Integer> ids = new ArrayList<>();
        for (int i = 0; i < selectCount; i++) {
            for (int j = 0; j < roomList.size(); j++) {
                if (roomList.get(j).getOrder()-1 == i) {
                    ids.add((int) roomList.get(j).getId());
                    break;
                }
            }
        }
        return ids;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            final Room room = roomList.get(position);
            RoomEditAdapter.ViewHolder myViewHolder = (RoomEditAdapter.ViewHolder) holder;

            String name = room.getName();

            String specifiedName = getSpecifiedName(name);
            myViewHolder.cb_room.setText(specifiedName);

            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            myViewHolder.cb_room.setCompoundDrawables(drawable, null, null, null);

            if (room.getOrder() > 0) {
                myViewHolder.cb_room.setChecked(true);
                myViewHolder.tv_roomOrder.setText(room.getOrder() + "");
                myViewHolder.tv_roomOrder.setBackgroundResource(R.drawable.shape_oval_gradient_check);
            } else {
                myViewHolder.cb_room.setChecked(false);
                myViewHolder.tv_roomOrder.setBackgroundResource(R.drawable.room_tag_oval_default);
            }

            myViewHolder.ll_room.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (RomUtils.isFastClick()) {
                        return;
                    }
                    int order = room.getOrder();
                    if (order > 0) {
                        for (int i = 0; i < roomList.size(); i++) {
                            if (roomList.get(i).getOrder() > order) {
                                roomList.get(i).setOrder(roomList.get(i).getOrder() - 1);
                            }
                        }
                        room.setOrder(0);
                        selectCount--;
                    } else {
                        room.setOrder(++selectCount);
                    }
                    for (int i = 0; i < roomList.size(); i++) {
                        LogUtils.i(" room name " + roomList.get(i).getName() + " order : " + roomList.get(i).getOrder());
                    }
                    notifyDataSetChanged();
                }
            });
        }

    }

    public String getSpecifiedName(String name) {
         /*
        "yxroom_name_1" = "客厅";
        "yxroom_name_2" = "主卧";
        "yxroom_name_3" = "次卧";
        "yxroom_name_4" = "衣帽间";
        "yxroom_name_5" = "玄关";
        "yxroom_name_6" = "阳台";
        "yxroom_name_7" = "厨房";
        "yxroom_name_8" = "书房";
        "yxroom_name_9" = "餐厅";
        "yxroom_name_10" = "儿童房";
        "yxroom_name_11" = "卫生间";
        */
        String specifiedName = name;
        if ("yxroom_name_1".equals(name)) {
            specifiedName = mContext.getString(R.string.MM_MasterBedroom);
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_drawing_room_selector);
        } else if ("yxroom_name_2".equals(name)) {
            specifiedName = mContext.getString(R.string.MM_LivingRoom);
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_master_bedroom_selector);
        } else if ("yxroom_name_3".equals(name)) {
            specifiedName = mContext.getString(R.string.MM_SecondBedroom);
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_secondary_bedroom_selector);
        } else if ("yxroom_name_4".equals(name)) {
            specifiedName = mContext.getString(R.string.MM_Cloakroom);
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_cloak_room_selector);
        } else if ("yxroom_name_5".equals(name)) {
            specifiedName = mContext.getString(R.string.MM_Vestibule);
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_porch_selector);
        } else if ("yxroom_name_6".equals(name)) {
            specifiedName = mContext.getString(R.string.MM_Balcony);
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_balcony_selector);
        } else if ("yxroom_name_7".equals(name)) {
            specifiedName = mContext.getString(R.string.MM_Kitchen);
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_kitchen_selector);
        } else if ("yxroom_name_8".equals(name)) {
            specifiedName = mContext.getString(R.string.MM_StudyRoom);
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_study_selector);
        } else if ("yxroom_name_9".equals(name)) {
            specifiedName = mContext.getString(R.string.MM_DiningRoom);
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_dinning_room_selector);
        } else if ("yxroom_name_10".equals(name)) {
            specifiedName = mContext.getString(R.string.MM_KidBedroom);
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_children_room_selector);
        } else if ("yxroom_name_11".equals(name)) {
            specifiedName = mContext.getString(R.string.MM_Bathroom);
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_toilet_selector);
        } else {
            drawable = mContext.getResources().getDrawable(R.drawable.precinct_customize_selector);
        }
        return specifiedName;
    }

    @Override
    public int getItemCount() {
        return roomList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox cb_room;
        TextView tv_roomOrder;
        RelativeLayout ll_room;

        public ViewHolder(View itemView) {
            super(itemView);
            cb_room = itemView.findViewById(R.id.cb_room);
            tv_roomOrder = itemView.findViewById(R.id.tv_roomOrder);
            ll_room = itemView.findViewById(R.id.ll_room);
        }
    }
}
